package com.ecarx.vhaltests.hy11.testcases.notify;

import android.car.VehicleAreaType;
import android.car.VehiclePropertyType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;

import com.ecarx.vhaltests.CallbackTimer;
import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.automotive.VehicleArea;
import com.ecarx.xui.adaptapi.automotive.VehiclePropertyGroup;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

/**
 * 胎压/胎温异常报警通知 220 胎温报警
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class NotifyInfo220Test extends VhalUnitTestBase {
    private static int propertyId = 0x76ff
            | VehiclePropertyGroup.VENDOR
            | VehiclePropertyType.INT32_VEC
            | VehicleArea.GLOBAL;
    private CarPropertyManager.CarPropertyEventCallback propertyCbk;
    private static IpcpClient ipcpClient;
    private boolean isFirstCbk;
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static final int NOTIFY_ID = 220;
    private static final int STATUS_SHOW = 1;
    private static final int STATUS_CANCEL = 0;
    private static final String WARNING_KEY = "leFrntTireMsgTWarnFlg";
    private static final int VALUE_WARNING = 1;
    private static final int VALUE_NORMAL = 0;
    private CallbackTimer cbkTimer = new CallbackTimer();


    @BeforeClass
    public static void testInit() {
        setPreCondition(true);
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(propertyId, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertTrue(isAvail);
        delay();
    }

    @Test
    public void test001_NotifyInfo220_Show() {
        mockCemValue(VALUE_WARNING);
        delay();
        int[] values = mCarPropertyMgr.getIntArrayProperty(propertyId, AREA_ID);
        Assert.assertEquals(NOTIFY_ID, values[0]);
        Assert.assertEquals(STATUS_SHOW, values[1]);

    }

    @Test
    public void test002_NotifyInfo220_Cancel() {
        mockCemValue(VALUE_NORMAL);
        delay();
        int[] values = mCarPropertyMgr.getIntArrayProperty(propertyId, AREA_ID);
        Assert.assertEquals(NOTIFY_ID, values[0]);
        Assert.assertEquals(STATUS_CANCEL, values[1]);
    }

    @Test
    public void test003_NotifyInfo220_Cbk() {
        isFirstCbk = true;
        propertyCbk = new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                if (carPropertyValue.getPropertyId() == propertyId && carPropertyValue.getAreaId() == AREA_ID) {
                    if (isFirstCbk) {
                        isFirstCbk = false;
                    } else {
                        Integer[] values = (Integer[]) carPropertyValue.getValue();
                        Assert.assertEquals(NOTIFY_ID, values[0].intValue());
                        Assert.assertEquals(STATUS_SHOW, values[1].intValue());
                        mCarPropertyMgr.unregisterCallback(propertyCbk);
                        cbkTimer.finish();
                    }
                }
            }

            @Override
            public void onErrorEvent(int i, int i1) {

            }
        };
        mCarPropertyMgr.registerCallback(propertyCbk, propertyId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        delay();
        mockCemValue(VALUE_WARNING);
        cbkTimer.start();
        delay(500);
    }


    private static void setPreCondition(boolean isAvail) {
        boolean isFirstSet = false;
        if (ipcpClient == null) {
            ipcpClient = new IpcpClient();
            isFirstSet = true;
        }
        int cemSvcId = 37;
        int cemOperId = 999;
        int[] cemOperIds = {999};
        int ccSvcId = 135;
        int ccOperId = 200;
        int[] ccOperIds = {200};
        int[] configs = {19};
        int[] values = {isAvail ? 2 : 1};
        if (isFirstSet) {
            ipcpClient.setConfig(cemSvcId, cemOperIds);
            ipcpClient.setConfig(ccSvcId, ccOperIds);
        }
        boolean ccRet = ipcpClient.setCarconfigSignal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccRet);
    }

    private static void mockCemValue(int value) {
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1UsgModSts", WARNING_KEY};
        int[] cemValues = {VehicleConstants.USG_MODE_DRIVING, value};
        boolean cemRet = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        Assert.assertTrue(cemRet);
    }
}